package com.zys.sac.core.ext;

import com.zys.sac.core.helper.SecurityContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhongjunkai on 2022/5/9.
 */
public class JwtSecurityContext implements SecurityContext {

    @Override
    public String currentUsername(HttpServletRequest request) {
        UserDetails principal = (UserDetails) getPrincipal();
        return principal.getUsername();
    }

    @Override
    public Object getPrincipal() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            return authentication.getPrincipal();
        }
        return null;
    }
}
